/** 
 * @author khathirdstep1025
 * Servlet handles trainees Save and Update.
 */

package com.kha.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kha.Database;
import com.kha.Trainee;

/**
 * Servlet implementation class TraineeServlet
 */
@WebServlet("/TraineeServlet")
public class TraineeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// Database config
	private String CONNECTION_URL = "";
	private String DB_Driver = "";

	/**
	 * Servlet Init.
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {

		// Get data stored in Servlet Context.
		ServletContext context = config.getServletContext();

		CONNECTION_URL = context.getInitParameter("CONNECTION_URL")
				+ "?user=root&;password=";
		DB_Driver = context.getInitParameter("DB_Driver");

		super.init(config);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// do the same thing as doPost
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String hidden = request.getParameter("hidden");

		if (hidden != null && "update".equals(hidden)) {

			// Update triggered
			// If user press Update button, redirect back to homepage index.jsp.
			response.sendRedirect("index.jsp");

		}

		// Get session object.
		HttpSession session = request.getSession(true);

		Trainee trainee = (Trainee) session.getAttribute("trainee");

		if (hidden != null && "save".equals(hidden)) {

			Database db = new Database(CONNECTION_URL, DB_Driver);

			// save triggered.

			// Check if account existed.

			int count = 0;

			try {
				count = db.Count(trainee.getAccount(), "account", "trainee");
			} catch (InstantiationException e1) {
			} catch (IllegalAccessException e1) {
			} catch (ClassNotFoundException e1) {
			} catch (SQLException e1) {
			}

			if (count > 0) {
				// Existed.
				// Updated instead.
				try {
					db.Update(trainee);
				} catch (InstantiationException e) {
				} catch (IllegalAccessException e) {
				} catch (ClassNotFoundException e) {
				} catch (SQLException e) {
				}

				// Info user.
				String msg = "Update successfully";

				request.setAttribute("message", msg);

			} else { // Insert new.

				try {
					db.Save(trainee);
				} catch (InstantiationException e) {
				} catch (IllegalAccessException e) {
				} catch (ClassNotFoundException e) {
				} catch (SQLException e) {
				}

				// Info user.
				String msg = "Save successfully";
				request.setAttribute("message", msg);

			}

			// Get list of trainees display on page.
			ArrayList<Trainee> list = null;
			try {
				list = db.getTraineeList();
			} catch (InstantiationException e) {
			} catch (IllegalAccessException e) {
			} catch (ClassNotFoundException e) {
			} catch (SQLException e) {
			}

			if (list != null) {

				request.setAttribute("result", list);

				// Redirect to trainee page with request.
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("trainees.jsp");
				dispatcher.forward(request, response);
			
			}

		}
	}
}